﻿using Developer.Code;
using Developer.Service;
using Super.Framework.Common.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Developer.Web.Controllers
{
    public class ProfileController : BaseController
    {
        public ActionResult MyProfile()
        {
            var id = Query.Value("uid").ToInt();
            var user = UserService.GetAll().Find(id);
            return View(user);
        }

        public ActionResult MyFavorites()
        {
            var id = Query.Value("uid").ToInt();
            var fa = FavoriteService.GetAll().Where(a => a.UserId == id);
            return View(fa);
        }

        public ActionResult MyMessage()
        {
            var id = Query.Value("uid").ToInt();
            var message = MessageService.GetAll().Where(a => a.UserId == id);
            return View(message);
        }
        [HttpPost]
        public JsonResult SetMessageReaded()
        {
            var id = Query.Value("mid").ToInt();
            try
            {
                MessageService.SetReaded(id);
                return Json(new { success = "成功"}, JsonRequestBehavior.AllowGet);
            }catch(Exception e){
                return Json(new { error = e.ToString() }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult EditMyProfile()
        {
            var uid = Query.Value("uid").ToInt();
            var user = UserService.GetAll().Find(uid);
            return View(user);
        }

        [HttpPost]
        public ActionResult EditMyProfile(int? z)
        {
            var uid = Query.Value("uid").ToInt();
            var user = new Entity.Users();
            if(uid > 0)
            {
                user = UserService.GetAll().Find(uid);
                user.HeadImg = Query.Value("HeadImg");
                user.NickName = Query.Value("NickName");
                UserService.Change(user);
            }
            return Redirect("/Profile/MyProfile?uid="+uid);
        }
    }
}